/**
* 左边菜单
*/
<template>
  <el-menu default-active="2" :collapse="collapsed" @select="handleSelect" @open="handleOpen"
           @close="handleClose" collapse-transition router :default-active="$route.path" unique-opened class="el-menu-vertical-demo"
           background-color="#334157" text-color="#fff" active-text-color="#ffd04b">
    <div class="logobox">
      <img class="logoimg" src="../assets/img/logo.png" alt="">
      <div class="logo-name" v-show="!collapsed">个人档案</div>
    </div>
    <el-submenu v-for="menu in allmenu" :key="menu.menuid" :index="menu.menuname">
      <template slot="title">
        <i class="iconfont" :class="menu.icon"></i>
        <span>{{menu.menuname}}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item v-for="chmenu in menu.menus" :index="'/'+chmenu.url" :key="chmenu.menuid">
          <i class="iconfont" :class="chmenu.icon"></i>
          <span slot="title">{{chmenu.menuname}}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>
<script>
  import {menu} from '../api/userMG'

  export default {
    name: 'leftnav',
    data() {
      return {
        collapsed: false,
        isCollapse: true,
        path: '/',
        model: '/',
        allmenu: [],
        tabs: [
          {label: '首页', name: '/'}
        ]
      }
    },
    // 创建完毕状态(里面是操作)
    created() {
      // 获取图形验证码
      let res = {
        success: true,
        data: [
          {
            menuid: 1,
            icon: 'li-icon-xiangmuguanli',
            menuname: '基础管理',
            hasThird: null,
            url: null,
            menus: [
              {
                menuid: 2,
                icon: 'icon-cat-skuQuery',
                menuname: '商品管理',
                hasThird: 'N',
                url: 'goods/Goods',
                menus: null
              }
            ]
          },
          {
            menuid: 33,
            icon: 'li-icon-dingdanguanli',
            menuname: '订单管理',
            hasThird: null,
            url: null,
            menus: [
              {
                menuid: 34,
                icon: 'icon-order-manage',
                menuname: '交易订单',
                hasThird: 'N',
                url: 'pay/Order',
                menus: null
              }
            ]
          },
          {
            menuid: 71,
            icon: 'li-icon-xitongguanli',
            menuname: '系统管理',
            hasThird: null,
            url: null,
            menus: [
              {
                menuid: 72,
                icon: 'icon-cus-manage',
                menuname: '用户管理',
                hasThird: 'N',
                url: 'system/user',
                menus: null
              },
              {
                menuid: 1000,
                icon: 'icon-cus-manage',
                menuname: '菜单管理01',
                hasThird: 'N',
                url: 'permission/menu',
                menus: null
              },
              {
                menuid: 174,
                icon: 'icon-cms-manage',
                menuname: '菜单管理',
                hasThird: 'N',
                url: 'system/Module',
                menus: null
              },
              {
                menuid: 73,
                icon: 'icon-news-manage',
                menuname: '角色管理',
                hasThird: 'N',
                url: 'system/Role',
                menus: null
              },
              {
                menuid: 74,
                icon: 'icon-cs-manage',
                menuname: '公司管理',
                hasThird: 'N',
                url: 'system/Dept',
                menus: null
              },
              {
                menuid: 75,
                icon: 'icon-promotion-manage',
                menuname: '系统环境变量',
                hasThird: 'N',
                url: 'system/Variable',
                menus: null
              },
              {
                menuid: 76,
                icon: 'icon-cms-manage',
                menuname: '权限管理',
                hasThird: 'N',
                url: 'system/Permission',
                menus: null
              }
            ]
          },
          {
            menuid: 128,
            icon: 'li-icon-shangchengxitongtubiaozitihuayuanwenjian91',
            menuname: '支付管理',
            hasThird: null,
            url: null,
            menus: [
              {
                menuid: 129,
                icon: 'icon-provider-manage',
                menuname: '支付配置信息',
                hasThird: 'N',
                url: 'machine/MachineConfig',
                menus: null
              },
              {
                menuid: 175,
                icon: 'icon-provider-manage',
                menuname: '支付配置',
                hasThird: 'N',
                url: 'pay/Config',
                menus: null
              }
            ]
          },
          {
            menuid: 150,
            icon: 'li-icon-shangchengxitongtubiaozitihuayuanwenjian91',
            menuname: '图表',
            hasThird: null,
            url: null,
            menus: [
              {
                menuid: 159,
                icon: 'icon-provider-manage',
                menuname: '数据可视化',
                hasThird: 'N',
                url: 'charts/statistics',
                menus: null
              }
            ]
          }
        ],
        msg: 'success'
      }
      this.allmenu = res.data

      // menu(localStorage.getItem('logintoken'))
      //   .then(res => {
      //     console.log(JSON.stringify(res))
      //     if (res.success) {
      //       this.allmenu = res.data
      //     } else {
      //       this.$message.error(res.msg)
      //       return false
      //     }
      //   })
      //   .catch(err => {
      //     this.$message.error('菜单加载失败，请稍后再试！')
      //   })
      // 监听
      this.$root.Bus.$on('toggle', value => {
        this.collapsed = !value
      })
    },
    methods: {
      // 菜单
      handleOpen(key, keyPath) {
        // console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        // console.log(key, keyPath);
      },
      handleSelect(key) {//获取当前选中菜单的基本信息
        // console.log(this);
        var self = this
        setTimeout(() => {
          // 名字
          let label = self.$route.name
          // 路由
          let path = self.$route.path
          // 长度
          let length = self.tabs.length
          // tabs数组
          let tab = self.tabs
          // // 定义个空数组，用来存放往session存放的tabs
          // var tab2 = []
          // 定义函数，如果选择的菜单在tabs数组存在则返回‘1’
          function currentTab() {
            for (let i = 0; i < length; i++) {
              if (tab[i].label == label) {
                return 1
              }
            }
          }

          // 执行刚刚定义的函数
          var currentTab = currentTab()
          // 如果执行函数返回值不为‘1’（选中的菜单是新菜单）
          if (currentTab !== 1) {
            // 添加菜单到数组
            self.tabs.push({
              label: label,
              name: path
            })
            self.model = path
            /*
                把选项卡存到session，使其刷新选项卡也在
            */
            // 定义个对象存放新菜单的数据
            let tab1 = [{
              label: label,
              name: path
            }]
            // 获取session里，标签页的数组
            let tabs = sessionStorage.getItem('tabs')
            // 如果session没有数据，则添加刚刚定义对象

            if (tabs == null) {
              // 把对象转字符串添加进session
              window.sessionStorage.setItem('tabs', JSON.stringify(tab1))
            } else {
              // 如果有对象，取出session来在，后面添加刚刚定义的对象
              // 取出session中的tab字符串，把他字符串转对象
              var tabs = JSON.parse(sessionStorage.getItem('tabs'))
              // 定义当前点击的菜单路由
              let path = self.$route.path
              // 如果点击的路由没有存在session里
              // 定义函数，如果选择的菜单在session的tabs数组存在则返回‘1’
              function b() {
                for (let i = 0; i < tabs.length; i++) {
                  if (tabs[i].name == path) {
                    return 1
                  }
                }
              }

              // 执行刚刚定义的函数
              var b = b()
              // 如果执行函数返回值不为‘1’（选中的菜单是新菜单）
              if (b !== 1) {
                tabs.push({
                  label: label,
                  name: path
                })
                window.sessionStorage.setItem('tabs', JSON.stringify(tabs))
                self.resetSetItem('tabs', JSON.stringify(tabs))
              }
            }
          } else {
            self.model = path
          }
          // 把当前选中菜单的path存到session
          window.sessionStorage.setItem('path', this.model)
        }, 100)
      }
    }
  }
</script>
<style>
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 240px;
    min-height: 400px;
  }

  .el-menu-vertical-demo:not(.el-menu--collapse) {
    border: none;
    text-align: left;
  }

  .el-menu-item-group__title {
    padding: 0px;
  }

  .el-menu-bg {
    background-color: #1f2d3d !important;
  }

  .el-menu {
    border: none;
  }

  .logobox {
    height: 40px;
    line-height: 40px;
    color: #9d9d9d;
    font-size: 20px;
    text-align: center;
    padding: 20px 0px;
  }

  .logoimg {
    height: 40px;
  }

  .logo-name {
    display: inline-block;
    font-size: 24px;
    position: relative;
    color: #eee;
    top: -7px;
  }
</style>
